//
//  LeftViewController.m
//  BeanTrip
//
//  Created by houlei on 15/7/7.
//  Copyright (c) 2015年 PandaGroup. All rights reserved.
//

#import "LeftViewController.h"
#import "FourthViewController.h"
#import "HelpViewController.h"
#import "UsViewController.h"

#define WIDTH ([UIScreen mainScreen].bounds.size.width)
#define HEIGHT ([UIScreen mainScreen].bounds.size.height)

@interface LeftViewController ()

@end

@implementation LeftViewController


- (void)dealloc
{
    [_tableView release];
    [super dealloc];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.tableView = ({
        UITableView *tableV = [[UITableView alloc] initWithFrame:CGRectMake(0, (HEIGHT - 54 * 5) / 2.0f, WIDTH, 54 * 5) style:UITableViewStylePlain];
        tableV.autoresizingMask = UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleWidth;
        tableV.delegate = self;
        tableV.dataSource = self;
        tableV.opaque = NO;
        tableV.backgroundColor = [UIColor clearColor];
        tableV.separatorStyle = UITableViewCellSeparatorStyleNone;
        tableV.bounces = NO;
        tableV;
    });
    [self.view addSubview:self.tableView];
    [_tableView release];
}
//区
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
//row
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 3;
}
//cell显示内容
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdentifier = @"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
        cell.backgroundColor = [UIColor clearColor];
        cell.textLabel.font = [UIFont fontWithName:@"HelveticaNeue" size:21];
        cell.textLabel.textColor = [UIColor whiteColor];
        cell.textLabel.highlightedTextColor = [UIColor lightGrayColor];
        cell.selectedBackgroundView = [[UIView alloc] init];
    }
    NSArray *titles = @[@"使用说明", @"清除缓存", @"关于我们"];
//    NSArray *images = @[@"IconHome", @"IconCalendar", @"IconProfile", @"IconSettings", @"IconEmpty"];
    cell.textLabel.text = [titles objectAtIndex:indexPath.row];
//    cell.imageView.image = [UIImage imageNamed:images[indexPath.row]];
    
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 54;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{

    
    if (indexPath.row == 0) {
        HelpViewController *helpVC = [[HelpViewController alloc] init];
        UINavigationController *naVC = [[UINavigationController alloc] initWithRootViewController:helpVC];
        [helpVC setModalTransitionStyle:UIModalTransitionStyleCoverVertical];
        [self presentViewController:naVC animated:YES completion:^{
            
            
        }];
        [naVC release];
        [helpVC release];
    }
    if (indexPath.row == 1) {
        UIAlertView *alertV = [[UIAlertView alloc] initWithTitle:@"提示" message:@"确定要清除缓存吗?" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
        [alertV show];
        alertV.tag = 1701;
        [alertV release];
    }
    if (indexPath.row == 2) {
        UsViewController *usVC = [[UsViewController alloc] init];
        [usVC setModalTransitionStyle:UIModalTransitionStyleCoverVertical];
        UINavigationController *naVC = [[UINavigationController alloc] initWithRootViewController:usVC];
        [self presentViewController:naVC animated:YES completion:^{

    
        }];

        

    }
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (alertView.tag == 1701) {
        if (buttonIndex == 1) {
            NSLog(@"开始清除缓存");
            [[SDImageCache sharedImageCache] clearDisk];
            NSString *str1 = [self getCachesPath];
            CGFloat num = [self fileSizeAtPath:str1];
            NSString *str = [NSString stringWithFormat:@"缓存清除成功, 大小为%.2fMB", num/1024];
            UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"" message:str delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
            [alertView show];
        }
    }
}

//=====================
//获取缓存文件路径
-(NSString *)getCachesPath{
    // 获取Caches目录路径
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
    NSString *cachesDir = [paths objectAtIndex:0];
    
    NSString *filePath = [cachesDir stringByAppendingPathComponent:@"com.hackemist.SDWebImageCache.default"];
    
    return filePath;
}


///计算缓存文件的大小的M
- (long long) fileSizeAtPath:(NSString*) filePath{
    NSFileManager* manager = [NSFileManager defaultManager];
    if ([manager fileExistsAtPath:filePath]){
        return [[manager attributesOfItemAtPath:filePath error:nil] fileSize];
    }
    return 0;
}
//=====================


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
